package com.LeeCode;

/**
 * 删除并获得点数
 */

public class Code740 {
    public static void main(String[] args) {
        int[] nums = {2, 2, 3, 3, 3, 4};
        System.out.println(new Code740().deleteAndEarn(nums));
    }

    public int deleteAndEarn(int[] nums) {
        int maxVal = nums[0];
        for (int num : nums) {
            maxVal = Math.max(maxVal, num);
        }
        int[] sum = new int[maxVal + 1];
        for (int num : nums) {
            sum[num] += num;
        }
        int val1 = sum[0], val2 = Math.max(sum[1], sum[0]);
        for (int i = 2; i <= maxVal; i++) {
            int tmp = Math.max(val2, val1 + sum[i]);
            val1 = val2;
            val2 = tmp;
        }
        return val2;
    }
}
